package com.cpit.platform.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cpit.platform.common.constant.SystemConstant;
import com.cpit.platform.common.entity.TableDataQueryCondition;
import com.cpit.platform.common.entity.TableJsonData;
import com.cpit.platform.dao.MainPageItemRepository;
import com.cpit.platform.dto.MainPageItem;
import com.cpit.platform.dto.User;
import com.cpit.platform.log.LogOperator;
import com.cpit.platform.security.service.UserInfo;
import com.cpit.platform.service.MainPageService;
import com.cpit.platform.web.controller.AuthController;

@Service
@Transactional
public class MainPageServiceImpl implements MainPageService {
	
	private final String loggerName = this.getClass().getName();
	
	@Autowired
	private MainPageItemRepository srep = null;
	
	@Autowired
	private AuthController authCon = null;
	
	public List<MainPageItem> findAll() {
		return srep.findAll();
	}
	
	public MainPageItem findByID(Integer sysId){
		return srep.findById(sysId);
	}
	
	public void create(MainPageItem data) throws Exception {
		if(data.getId() != null && data.getId() > 0) {
			LogOperator.info(loggerName, "create role, need delete role anth mappiong " + data.getId());
			//rtoARep.deleteByRoleId(data.getId());
		}
		UserInfo ui = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		User usercur = ui.getUser();
		if(usercur == null) {
			throw new Exception("用户未登录");
		}
		data.setUser(usercur.getUsername());
		MainPageItem fbN = srep.findByName(data.getName());
		if(fbN != null) {
			throw new Exception("同名角色已经存在！");
		}
		srep.save(data);
	}
	
	public void delete(MainPageItem data) throws Exception {
		srep.delete(data);
	}
	
	public MainPageItem findById(Integer id) {
		return srep.findById(id);
	}
	

	public List<MainPageItem> findByCondition(int page, 
			int size, 
			String order, 
			String orderBy, 
			String sysName) throws Exception {
		if(orderBy == null || orderBy.length() == 0) {
			orderBy = "id";
		}
		UserInfo ui = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		User usercur = ui.getUser();
		if(usercur == null) {
			throw new Exception("用户未登录");
		}
		Direction der = order.equals(SystemConstant.DIRECTION_ASC) ? Direction.ASC : Direction.DESC;
		PageRequest request = new PageRequest(page - 1, size, new Sort(der, orderBy));
		Page<MainPageItem> ret = srep.findByUser(usercur.getUsername(), request);
		return ret.getContent();
	}

	@SuppressWarnings("rawtypes")
	public TableJsonData findByConditionJqg(TableDataQueryCondition cond) throws Exception {
		if(cond.getSidx() == null || cond.getSidx().length() == 0) {
			cond.setSidx("id");
		}
		Direction der = null;
		if(cond.getSord() == null || cond.getSord().length() == 0) {
			der = Direction.ASC;
		} else 
			der = cond.getSord().equals(SystemConstant.DIRECTION_ASC) ? Direction.ASC : Direction.DESC;
		PageRequest request = new PageRequest(cond.getPageNumber() - 1, cond.getRowsNumber(), 
				new Sort(der, cond.getSidx()));
		UserInfo ui = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		User usercur = ui.getUser();
		if(usercur == null) {
			throw new Exception("用户未登录");
		}
		Page<MainPageItem> pr = null;
		pr = srep.findByUser(usercur.getUsername(), request);
		return new TableJsonData(cond, pr.getTotalElements(), pr.getContent());
	}

	
	
	
}
